/*
 * Copyright (c) 2016 Kevin Herron
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v1.0 which accompany this distribution.
 *
 * The Eclipse Public License is available at
 *   http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *   http://www.eclipse.org/org/documents/edl-v10.html.
 */

package com.dayu.unitalk.ua.server;

import static org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte;

import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import org.eclipse.milo.opcua.sdk.core.AccessLevel;
import org.eclipse.milo.opcua.sdk.server.Session;
import org.eclipse.milo.opcua.sdk.server.api.nodes.VariableNode;
import org.eclipse.milo.opcua.sdk.server.nodes.AttributeContext;
import org.eclipse.milo.opcua.sdk.server.nodes.delegates.AttributeDelegate;
import org.eclipse.milo.opcua.sdk.server.nodes.delegates.DelegatingAttributeDelegate;
import org.eclipse.milo.opcua.stack.core.UaException;
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte;

public class RestrictedAccessDelegate extends DelegatingAttributeDelegate {

	private static final Set<AccessLevel> INTERNAL_ACCESS = AccessLevel.READ_WRITE;

	private final Function<Object, Set<AccessLevel>> accessLevelsFn;

	public RestrictedAccessDelegate(Function<Object, Set<AccessLevel>> accessLevelsFn) {
		this(null, accessLevelsFn);
	}

	public RestrictedAccessDelegate(AttributeDelegate parent, Function<Object, Set<AccessLevel>> accessLevelsFn) {
		super(parent);

		this.accessLevelsFn = accessLevelsFn;
	}

	@Override
	public UByte getUserAccessLevel(AttributeContext context, VariableNode node) throws UaException {

		Optional<Object> identity = context.getSession().map(Session::getIdentityObject);
		Set<AccessLevel> accessLevels = identity.map(accessLevelsFn).orElse(INTERNAL_ACCESS);

		return ubyte(AccessLevel.getMask(accessLevels));
	}

}
